using System;
using System.Collections.Generic;
using System.Text;

namespace GuavaData
{
    public class AddTable : Operator
    {
        // DDL statement for adding a new table

        #region Fields
        public bool EnforcePrimaryKey;
        
        string table;
        public string Table
        {
            get { return table; }
            set { table = value; }
        }

        List<string> columns;
        public List<string> Columns
        {
            get { return columns; }
            set { columns = value; }
        }

        List<Domain.Domain> domains;
        public List<Domain.Domain> Domains
        {
            get { return domains; }
            set { domains = value; }
        }

        List<string> keys;
        public List<string> Keys
        {
            get { return keys; }
            set { keys = value; }
        }
        #endregion Fields

        public AddTable(string table)
        {
            this.table = table;
            this.columns = new List<string>();
            this.domains = new List<GuavaData.Domain.Domain>();
            this.keys = new List<string>();
            this.parent = null;
            this.EnforcePrimaryKey = true;
        }

        public AddTable(string table, IEnumerable<string> columns, IEnumerable<Domain.Domain> domains, IEnumerable<string> keys)
        {
            this.table = table;
            this.columns = new List<string>(columns);
            this.domains = new List<GuavaData.Domain.Domain>(domains);
            this.keys = new List<string>(keys);
            this.parent = null;
            this.EnforcePrimaryKey = true;

            // Dummy check
            if (this.columns.Count != this.domains.Count)
                throw new Exception("Number of columns does not match number of domains in Add Table statement.");
        }

        public override void NotifyChange(Operator old, Operator newOp)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        public override bool isQuery
        {
            get
            {
                return false;
            }
        }
        public override SqlText Visit(Provider qv)
        {
            return qv.Transform(this);
        }
        public override void Visit(Transform t, Command c)
        {
            t.Expand(this, c);
        }
        public override Operator Clone()
        {
            return new AddTable(this.table, this.columns, this.domains, this.keys);
        }
        public void AddColumn(string column, Domain.Domain domain, bool pk)
        {
            this.columns.Add(column);
            this.domains.Add(domain);
            if (pk)
                this.keys.Add(column);
        }
        public override void Visit(Channel c, string table)
        {
            c.MapAliases(this, table);
        }
        public override string ToString()
        {
            return "Add Table \"" + table + "\"";
        }

    }
}
